<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\exception\UnSupportedOperationException;
use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;

/**
 * factory action
 * @package admin\action
 * @author yangjian<yangjian102621@gmail.com>
 */
class FactoryAction extends CommonAction {

    protected $serviceBean = 'product.factory.service';
    protected $actionTitle = '厂家';

    //数据列表页面
    public function index( HttpRequest $request ) {
        $name = $request->getParameter('name', 'trim');
        $status = $request->getParameter('status', 'trim');

        if ( $status === '' ) {
            $status = '-1';
            $request->addParameter('status', '-1');
        }
        $conditions = array();
        if ( $name ) $conditions['name'] = "%{$name}%";
        if ( $status != '-1' ) $conditions['status'] = $status;

        $this->setConditions($conditions);
        $this->assign('tableHeader', array('厂家名称','牌号数量（个）','当前状态'));
        if ( $request->getParameter('__export') == 1 ) { //导出数据
            $this->export($request);

        } else { //加载模板
            parent::index($request);
            $this->setView('product/factory_index');
            $this->assign("title", $this->actionTitle."列表");
            $this->assign('breadcrumb', array('产品管理',$this->getTemplateVar('title')));
            $this->assign('breadUrl', array('javascript:;','javascript:;'));
        }

        }

    //数据添加页面
    public function add(HttpRequest $request) {
        throw new UnSupportedOperationException('暂时不支持该操作.');
    }

    //数据编辑页面
    public function edit(HttpRequest $request) {

        $id = $request->getParameter('id', 'intval');
        if ( $id <= 0 ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, self::NO_RECORDS);
        }
        $service = Beans::get($this->getServiceBean());
        $item = $service->getItem($id);
        if ( empty($item) ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, self::NO_RECORDS);
        }
        AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS, null, $item);
    }

    //插入数据操作
    public function insert(HttpRequest $request) {

        $data = $request->getParameter('data');
        parent::__insert($data);
    }

    //更新数据操作
    public function update(HttpRequest $request) {

        $data = $request->getParameter('data');
        $id = $request->getParameter('id', 'intval');
        if ( $id <= 0 ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, self::NO_RECORDS);
        }
        parent::__update($data, $id);

    }

    //导出数据
    protected function export(HttpRequest $request) {
        if( $request->getParameter('__export') == 1 ){
            $ids = $request->getParameter('ids');
            if( $ids ){
                $conditions['id']['$in']=$ids;
                $this -> setConditions($conditions);
            }
            $this->setPagesize(getConfig('export_max_count'));
            $service = Beans::get($this->getServiceBean());
            $items = $service->where($this->getConditions())->limit(1, $this->getPagesize())->sort(array('addtime' => -1))->find();
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["name"];
                $tmp[] = $v["grade_num"];
                $tmp[] = $v['status'] == 1 ? '启用' : '禁用';
                $excel[] = $tmp;
            }
            $header = $this->getTemplateVar('tableHeader');
            phpexcel("厂家列表",$header,$excel);
            exit();
        }
    }
}
